"********************************plugins**************************************
"git clone https://github.com/gmarik/vundle.git  ./vimfiles/bundle/vundle
" curl -fLo ~/.vim/autoload/plug.vim --create-dirs \\n    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
" mkdir ~/.vim/pluged
" composer global require "techlivezheng/phpctags"
call plug#begin('~/.vim/pluged')

"tools
Plug 'scrooloose/nerdtree'
Plug 'jistr/vim-nerdtree-tabs'
Plug 'scrooloose/nerdcommenter'
Plug 'Raimondi/delimitMate'
Plug 'scrooloose/syntastic'
Plug 'godlygeek/tabular'
Plug 'tpope/vim-surround'
"Plug 'Shougo/neocomplcache'
Plug 'SirVer/ultisnips'
Plug 'ctrlpvim/ctrlp.vim'
Plug 'Lokaltog/vim-easymotion'
Plug 'nathanaelkane/vim-indent-guides'
"Plug 'Valloric/YouCompleteMe'
Plug 'Shougo/vimproc.vim',{'do': 'make'}
Plug 'Shougo/vimshell.vim'

"colorscheme
"Plug 'tomasr/molokai'
Plug 'altercation/vim-colors-solarized'

"lang
Plug 'pangloss/vim-javascript'
Plug 'elzr/vim-json'
Plug 'maksimr/vim-jsbeautify'
Plug 'othree/javascript-syntax.vim'
Plug 'myhere/vim-nodejs-complete'
Plug 'mattn/emmet-vim'
Plug 'othree/html5.vim'
Plug 'ap/vim-css-color'
Plug 'spf13/PIV'
Plug 'StanAngeloff/php.vim'
Plug 'plasticboy/vim-markdown'
Plug 'vim-ruby/vim-ruby'
Plug 'tpope/vim-rails'
Plug 'leafgarland/typescript-vim'

Plug 'nvie/vim-flake8'
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
Plug 'Valloric/YouCompleteMe'
call plug#end()

"********************************ui**************************************

"字体/配色
set background=dark
colorscheme solarized
set guifont=Source\ Code\ Pro\ for\ Powerline\ 12
set guifontwide=YouYuan,YaHei,Consolas:h12 

"去掉工具条、菜单栏、滚动条
set go=aAce              
set guioptions-=m 
set shm+=I
set guioptions-=T
set guitablabel=%N:%M%t

"高亮光标所在的行
set cursorline

"if has('gui_running')
"    "自动最大化窗口
""    au GUIEnter * simalt ~x
"    "给 Win32 下的 gVim 窗口设置透明度
""    au GUIEnter * call libcallnr("vimtweak.dll", "SetAlpha", 237)
"endif

"********************************language**************************************

"语言中文
language zh_CN.utf-8
language messages zh_CN.utf-8
set helplang=cn

"********************************encoding**************************************
"默认为 UTF-8 编码
set encoding=utf-8
set termencoding=utf-8
set fileencodings=ucs-bom,utf-8,cp936,big5,euc-jp,euc-kr,latin1

"********************************system**************************************

"共享剪贴板
set clipboard+=unnamed

"保留历史记录
set history=500

"导入删除菜单脚本，删除乱码的菜单
source $VIMRUNTIME/delmenu.vim

"导入正常的菜单脚本
source $VIMRUNTIME/menu.vim 
 
set nocompatible

"********************************setting**************************************
" 行控制
set linebreak
set textwidth=80
set wrap

"带如下字符不换行
set iskeyword+=_,$,@,%,#,-

"标签页
set tabpagemax=9
set showtabline=2

"控制台响铃
set noerrorbells
set novisualbell
set t_vb= "close visual bell
"行号和标尺
set number
set ruler
set rulerformat=%15(%c%V\ %p%%%)

"命令行于状态行
set ch=1
set ls=2 " 始终显示状态行
set wildmenu "命令行补全以增强模式运行

"高亮、实时搜索
set hlsearch  
set magic     
set showmatch 
set mat=2    
set incsearch
set ignorecase

"制表符
set tabstop=4
set expandtab
set smarttab
set shiftwidth=4
set softtabstop=4

"状态栏显示目前所执行的指令
set showcmd 

"缩进
set autoindent
set smartindent

"自动重新读入
set autoread

"插入模式下使用 <BS>、<Del> <C-W> <C-U>
set backspace=indent,eol,start

"设定在任何模式下鼠标都可用
set mouse=a

"自动改变当前目录
"set autochdir

"备份和缓存
set nobackup
set noswapfile

"自动完成
set complete=.,w,b,k,t,i
set completeopt=longest,menu

"定义 <Leader> 为逗号
let mapleader = ","
let maplocalleader = ","

"保证语法高亮
syntax enable
syntax on

filetype plugin indent on

" 配置文件.vimrc更改后自动重新载入使设置生效
autocmd! bufwritepost _vimrc source %

"********************************mapping**************************************

"标签页操作
nmap nt :tabnew<cr>
nmap tg :tabprevious<cr>
nmap nc :tabclose<cr>

"去掉查找后的高亮
nmap nh :noh<cr>

"转换文件类型
nmap 'h :set filetype=html<cr>
nmap 'j :set filetype=javascript<cr>
nmap 'c :set filetype=css<cr>
nmap 'p :set filetype=php<cr>

"在行尾添加;
nmap ; $A;<esc>

"在行尾添加，
nmap <leader><leader> $A,<esc>

"另存为
nmap ss :browse confirm saveas<cr>

"切换窗口
nmap tt <c-w>w

"nerdtree快捷键
nmap ne :NERDTreeToggle<cr>

"新窗口打开配置文件
nmap <leader>e :tabnew $VIM/_vimrc<cr>

"设置重新载入.vimrc快捷键
map <leader>r :source $VIM/_vimrc<cr>

" 按下 Q 不进入 Ex 模式，而是退出
nmap Q :x<cr>

"Tabularize插件对齐快捷键
nmap <Leader>te :Tabularize /=<cr>
nmap <Leader>tm :Tabularize /:<cr>
nmap <Leader>tc :Tabularize /\/\/<cr> "排序注释

"easymotion
nmap f <Plug>(easymotion-prefix)

"vimshell
nmap <Leader>ts :VimShellTab<cr>

"f8唤起命令行工具
nmap <f8> :!cmd <cr>

"Jsbeautify插件
nnoremap <F4> :call g:Jsbeautify()<CR>

"********************************plugin setting**************************************
"ultisnips
let g:UltiSnipsSnippetsDir = $VIM."/vimfiles/snippets/"

"NERDTree
let NERDTreeDirArrows=1 "目录箭头 1 显示箭头  0传统+-|号
let NERDTreeShowHidden = 0
let NERDTreeStatusline=''
let NERDTreeIgnore=['\.pyc$', '\.pyo$', '\.obj$', '\.o$', '\.so$', '\.egg$', '^\.git$', '^\.svn$', '^\.hg$', '\.lnk$']

"neocomplcache
let g:neocomplcache_enable_at_startup = 1
let g:neocomplcache_enable_camel_case = 1

" syntastic
"let g:syntastic_check_on_open = 1
let g:syntastic_error_symbol = '•'
let g:syntastic_warning_symbol = '!'
let g:syntastic_auto_loc_list = 1
let g:syntastic_loc_list_height = 5
let g:syntastic_enable_highlighting = 0
let g:syntastic_mode_map = { 'passive_filetypes': ['scss', 'slim'] }
let g:syntastic_javascript_syntax_checker="jshint"
let g:syntastic_javascript_jshint_args="--config ".$VIM."/vimfiles/syntax/.jshintrc"

"indent guide
let g:indent_guides_guide_size=1

"markdown
let g:vim_markdown_folding_disabled=1

"jsbeautify
au FileType javascript nmap <Leader>jj :call JsBeautify()<cr>
au FileType html nmap <Leader>jj :call HtmlBeautify()<cr>
au FileType css nmap <Leader>jj :call CSSBeautify()<cr>
au FileType javascript vnoremap <Leader>rj :call RangeJsBeautify()<cr>
au FileType html vnoremap <Leader>rj :call RangeHtmlBeautify()<cr>
au FileType css vnoremap <Leader>rj :call RangeCSSBeautify()<cr>

"********************************syntax**************************************
" 给各语言文件添加 Dict
au FileType html setlocal dict+=$VIM/vimfiles/dict/html.dict
au FileType html setlocal dict+=$VIM/vimfiles/dict/css.dict
au FileType html setlocal dict+=$VIM/vimfiles/dict/javascript.dict
au FileType css setlocal dict+=$VIM/vimfiles/dict/css.dict
au FileType javascript setlocal dict+=$VIM/vimfiles/dict/javascript.dict
au FileType javascript setlocal dict+=$VIM/vimfiles/dict/node.dict
au FileType php setlocal dict+=$VIM/vimfiles/dict/php.dict

" 将指定文件的换行符转换成 dos 格式
"au FileType php,javascript,html,xml,json,css,txt,vim,vimwiki set ff=dos

"********************************codeExec**************************************

fun! ExecuteScript()
    if &filetype == 'php'
        exe ':!php %'
    elseif &filetype == 'ruby'
        exe ':!ruby %'
    elseif &filetype == 'javascript'
        if expand('%') == 'gulpfile.js'
            exec ':!gulp'
        else
            exe ':!node %'
        endif
    elseif &filetype == 'typescript'
        exe ':make'
    endif
endfunction

au FileType php,javascript,ruby,typescript nmap <f5> :call ExecuteScript()<cr>

let g:airline_theme="luna" 
let g:airline_powerline_fonts = 1   

"youcompleteme
let g:ycm_global_ycm_extra_conf = '~/.vim/pluged/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'
" 不显示开启vim时检查ycm_extra_conf文件的信息
let g:ycm_confirm_extra_conf = 0
" 开启基于tag的补全，可以在这之后添加需要的标签路径
let g:ycm_collect_identifiers_from_tags_files = 1
" 开启语义补全
let g:ycm_seed_identifiers_with_syntax = 1
"注释和字符串中的文字也会被收入补全
let g:ycm_collect_identifiers_from_comments_and_strings = 0
" 输入第 2 个字符开始补全
let g:ycm_min_num_of_chars_for_completion= 2
" 禁止缓存匹配项,每次都重新生成匹配项
let g:ycm_cache_omnifunc=0
"在注释输入中也能补全
let g:ycm_complete_in_comments = 1
"在字符串输入中也能补全
let g:ycm_complete_in_strings = 1
"定义快捷健补全
let g:ycm_key_list_select_completion = ['<c-n>', '<Down>']
let g:ycm_key_list_previous_completion = ['<c-p>', '<Up>']
" 设置在下面几种格式的文件上屏蔽ycm
let g:ycm_filetype_blacklist = {
      \ 'tagbar' : 1,
      \ 'qf' : 1,
      \ 'notes' : 1,
      \ 'markdown' : 1,
      \ 'unite' : 1,
      \ 'text' : 1,
      \ 'vimwiki' : 1,
      \ 'pandoc' : 1,
      \ 'infolog' : 1,
      \ 'mail' : 1
      \}

"设置关健字触发补全
let g:ycm_semantic_triggers =  {
  \   'c' : ['->', '.', ' ', '(', '[', '&'],
  \   'objc' : ['->', '.', 're!\[[_a-zA-Z]+\w*\s', 're!^\s*[^\W\d]\w*\s',
  \             're!\[.*\]\s'],
  \   'ocaml' : ['.', '#'],
  \   'cpp,objcpp' : ['->', '.', '::'],
  \   'perl' : ['->'],
  \   'php' : ['->', '::'],
  \   'cs,java,javascript,typescript,d,python,perl6,scala,vb,elixir,go' : ['.'],
  \   'ruby' : ['.', '::'],
  \   'lua' : ['.', ':'],
  \   'erlang' : [':'],
  \ }
let g:ycm_cache_omnifunc = 1
let g:ycm_use_ultisnips_completer = 1
"定义函数跟踪快捷健
nnoremap <leader>jd :YcmCompleter GoToDefinitionElseDeclaration<CR>
